package com.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@Controller
public class FileController {
    //    上传文件到指定目录中
    @RequestMapping("/fileUpload")
//                                   与input名称一样
    public String fileUpload(MultipartFile file, HttpServletRequest request) throws IOException {
//        上传一个文件 真时地址
//        获取项目目录真实目录地址
        String path = request.getServletContext().getRealPath("/");
//         获取设置上传目录地址
        String upPath = "/static/upload/";
        if (!file.isEmpty()) {
//            判断是否有内容
//            判断文件名 todo 文件名加唯一时间戳
            String filname = file.getOriginalFilename();
            String filePath = path + upPath + filname;
            file.transferTo(new File(filePath));
            return "uploadSuccess";
        }
        return "uploadFail";
    }
}
